package com.liukai.user.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_commons.utils.MyInfoList;
import com.liukai.user.R;

import java.util.ArrayList;
import java.util.List;

public class MyInfoRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int ITEM_LOGOUT = 1;
    private static final int ITEM_OTHER = 2;

    // 我的信息界面功能列表
    private List<String> myInfoList;

    public MyInfoRecyclerAdapter(List<String> itemList){
        this.myInfoList = itemList;
    }

    @Override
    public int getItemViewType(int position) {
        String itemName = myInfoList.get(position);
        if (itemName.equals(Constans.LOGOUT)){
            return ITEM_LOGOUT;
        }else{
            return ITEM_OTHER;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_LOGOUT){
            view = View.inflate(parent.getContext(), R.layout.item_logout, null);
            return new LogoutViewHolder(view);
        }else{
            view = View.inflate(parent.getContext(), R.layout.item_setting, null);
            return new MyInfoViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        final String itemName = myInfoList.get(position);
        // item是退出登录
        if (itemName.equals(Constans.LOGOUT)){
            LogoutViewHolder logoutViewHolder = (LogoutViewHolder) viewHolder;
            logoutViewHolder.mBtnLogout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClick.onItemClickListener(itemName);
                }
            });
        }else {
            // item是其他项
            MyInfoViewHolder myInfoViewHolder = (MyInfoViewHolder) viewHolder;
            myInfoViewHolder.mTvSetting.setText(itemName);
            myInfoViewHolder.mBtnItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClick.onItemClickListener(itemName);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return myInfoList.size();
    }

    public static class MyInfoViewHolder extends RecyclerView.ViewHolder{

        public TextView mTvSetting;
        public RelativeLayout mBtnItem;

        public MyInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvSetting = itemView.findViewById(R.id.tv_setting);
            mBtnItem = itemView.findViewById(R.id.btn_item);
        }
    }

    public static class LogoutViewHolder extends RecyclerView.ViewHolder{


        public AppCompatButton mBtnLogout;

        public LogoutViewHolder(@NonNull View itemView) {
            super(itemView);
            mBtnLogout = itemView.findViewById(R.id.btn_logout);
        }
    }

    public interface ItemClick{
        void onItemClickListener(String itemName);
    }

    // item点击事件的接口
    private ItemClick mItemClick;
    // 将点击事件接口传递过来
    public void setItemClick(ItemClick itemClick) {
        mItemClick = itemClick;
    }

}
